// ClrButton.cpp : implementation file
//

#include "stdafx.h"
#include "ClrButton.h"
#include "DD_AnalyCfg.h"

extern COLORREF g_nRegionClr[MAX_DEF_REGION] ;
// CClrButton

IMPLEMENT_DYNAMIC(CClrButton, CButton)

CClrButton::CClrButton()
{
	m_TextClr = RGB(255,0,0);
	m_nFlag = -1 ;
	m_bSelect = false ;
}

CClrButton::~CClrButton()
{
}


BEGIN_MESSAGE_MAP(CClrButton, CButton)
	ON_WM_DRAWITEM()
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()

// CClrButton message handlers

DWORD CClrButton::OnDrawBackground(CDC* pDC, CRect* pRect)
{
	return 0 ;
}
void CClrButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	if(m_nFlag < 0 )
		return ;
	UINT   uStyle   =   DFCS_BUTTONPUSH; 
	//   If   drawing   selected,   add   the   pushed   style   to   DrawFrameControl. 
	if   (lpDrawItemStruct-> itemState   &   ODS_SELECTED) 
		uStyle   |=   DFCS_PUSHED; 

	//   Draw   the   button   frame. 
	::DrawFrameControl(lpDrawItemStruct-> hDC,   &lpDrawItemStruct-> rcItem,   
		DFC_BUTTON,   uStyle); 

	//   Get   the   button 's   text. 
	CString   strText; 
	GetWindowText(strText); 
	int nOldMode = ::SetBkMode(lpDrawItemStruct-> hDC,OPAQUE);
	COLORREF   crOldBkColor   =  ::SetBkColor(lpDrawItemStruct-> hDC, g_nRegionClr[m_nFlag]);// RGB(255,0,0)); 
	COLORREF   crOldColor   =   ::SetTextColor(lpDrawItemStruct-> hDC,  m_bSelect ? RGB(255,255,255) : RGB(0,0,0));//g_nRegionClr[m_nFlag]); 
	CFont hFont;
	CFont* pOldFont= NULL;
  	if (m_bSelect)
	{
		CString str ;
		str.Format("  %s  ",strText);
		::DrawText(lpDrawItemStruct-> hDC, str,   str.GetLength(), &lpDrawItemStruct-> rcItem,   DT_SINGLELINE|DT_VCENTER|DT_CENTER); 
	}
	else
	{
		::DrawText(lpDrawItemStruct-> hDC, strText,   strText.GetLength(), &lpDrawItemStruct-> rcItem,   DT_SINGLELINE|DT_VCENTER|DT_CENTER); 
	}
	::SetTextColor(lpDrawItemStruct-> hDC,   crOldColor); 
	SetBkMode(lpDrawItemStruct-> hDC,nOldMode);
}
void CClrButton::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	UINT   uStyle   =   DFCS_BUTTONPUSH; 
	ASSERT(lpDrawItemStruct-> CtlType   ==   ODT_BUTTON); 
	if   (lpDrawItemStruct-> itemState   &   ODS_SELECTED) 
		uStyle   |=   DFCS_PUSHED; 
	::DrawFrameControl(lpDrawItemStruct-> hDC,   &lpDrawItemStruct-> rcItem,   
		DFC_BUTTON,   uStyle); 
	CString   strText; 
	GetWindowText(strText); 

	//   Draw   the   button   text   using   the   text   color   red. 
	COLORREF   crOldColor   =   ::SetTextColor(lpDrawItemStruct-> hDC,   RGB(255,0,0)); 
	::DrawText(lpDrawItemStruct-> hDC,   strText,   strText.GetLength(),   
		&lpDrawItemStruct-> rcItem,   DT_SINGLELINE|DT_VCENTER|DT_CENTER); 
	::SetTextColor(lpDrawItemStruct-> hDC,   crOldColor); 

	CButton::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

HBRUSH CClrButton::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CButton::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  Change any attributes of the DC here
	pDC->SetTextColor(m_TextClr);
	// TODO:  Return a different brush if the default is not desired
	return hbr;
}
